package controller

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"github.com/gookit/validate"
	"strconv"
)

var (
	SUCCESS_CODE         = 200
	FAIL_CODE            = 40001
	SUCCESS_MSG          = "success"
	FAIL_MSG             = "fail"
	VALIDATOR_MAP        = map[string]string{"code": "701", "msg": "属性验证有误"}
	BINDING_PAMATERS_MAP = map[string]string{"code": "702", "msg": "参数绑定有误"}
)

/*父控制器 -- 继承beego.Controller*/
type BaseController struct {
	beego.Controller
}

/*统一返回*/
type Response struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

func (web BaseController) Ok(data any) {
	web.Data["json"] = &Response{Code: SUCCESS_CODE, Message: SUCCESS_MSG, Data: data}
	web.ServeJSON()
}

func (web BaseController) OkCode(code int, data any) {
	web.Data["json"] = &Response{Code: code, Message: SUCCESS_MSG, Data: data}
	web.ServeJSON()
}

func (web BaseController) OkCodeMsg(code int, msg string, data any) {
	web.Data["json"] = &Response{Code: SUCCESS_CODE, Message: msg, Data: data}
	web.ServeJSON()
}

func (web BaseController) Fail() {
	web.Data["json"] = &Response{Code: FAIL_CODE, Message: FAIL_MSG, Data: nil}
	web.ServeJSON()
}

func (web BaseController) FailData(data any) {
	web.Data["json"] = &Response{Code: FAIL_CODE, Message: FAIL_MSG, Data: data}
	web.ServeJSON()
}

func (web BaseController) FailCodeMsg(code int, msg string) {
	web.Data["json"] = &Response{Code: code, Message: msg, Data: nil}
	web.ServeJSON()
}

func (web BaseController) FailCodeValidMsg(code int, msg string, data any) {
	web.Data["json"] = &Response{Code: code, Message: msg, Data: data}
	web.ServeJSON()
}

func (web BaseController) FailWithValidatorData(validate *validate.Validation) {
	all := validate.Errors.All()
	one := validate.Errors.One()
	code, _ := strconv.ParseInt(VALIDATOR_MAP["code"], 10, 0)
	web.Data["json"] = &Response{Code: int(code), Message: one, Data: all}
	web.ServeJSON()
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc  获取登录的token中的用户id
 */
func (web BaseController) GetUserId() uint64 {
	data := web.Ctx.Input.GetData("userId")
	return data.(uint64)
}

func (web BaseController) GetUuid() string {
	data := web.Ctx.Input.GetData("uuid")
	return data.(string)
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc  获取登录的token中的用户名字
 */
func (web BaseController) GetUserName() string {
	data := web.Ctx.Input.GetData("userName")
	return data.(string)
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc  获取登录的token中的用户头像
 */
func (web BaseController) GetUserAvatar() string {
	data := web.Ctx.Input.GetData("avatar")
	return data.(string)
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 对param参数进行转换
 */
func (c *BaseController) ParseParamUint64(key string, def ...uint64) (uint64, error) {
	strv := c.Ctx.Input.Param(":" + key)
	if len(strv) == 0 && len(def) > 0 {
		return def[0], nil
	}
	return strconv.ParseUint(strv, 10, 64)
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc  对param参数进行转换
 */
func (c *BaseController) ParseParamInt64(key string, def ...int64) (int64, error) {
	strv := c.Ctx.Input.Param(":" + key)
	if len(strv) == 0 && len(def) > 0 {
		return def[0], nil
	}
	return strconv.ParseInt(strv, 10, 64)
}

/**
 * @author feige
 * @date 2023-10-08
 * @version 1.0
 * @desc json参数的绑定
 */
func (c *BaseController) BindJSONToStruct(obj interface{}) error {
	return json.Unmarshal(c.Ctx.Input.RequestBody, obj)
}
